/**
 * 
 */
package com.dangdang.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.smartstruts.action.Action;
import org.smartstruts.action.ActionForm;

import com.dangdang.domain.Category;
import com.dangdang.service.CategoryService;
import com.dangdang.service.ProductService;
import com.dangdang.service.ServiceFactory;
import com.dangdang.util.Constants;

/**
 * 商品分类控制中心
 * 
 * @author Wangpeihu
 * @team [Never give up]
 * @since JDK1.6(建议)
 * @date 2010-1-19
 */
public class CategoryAction extends Action {

	CategoryService cs = ServiceFactory.getCategoryService();
	ProductService ps = ServiceFactory.getProductService();

	/**
	 * 获得同级分类列表
	 * 
	 * @param from
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public String getSameCatLevel(ActionForm from, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("utf-8");
		Integer id = Integer.valueOf(request.getParameter("id"));
		List<Category> sameCategoryLevel = cs.findAllCategory(id);
		request.setAttribute("sameCategoryLevel", sameCategoryLevel);
	
		for (Category category : sameCategoryLevel) {
			System.out.println(category.getName());
		}
		return Constants.FORWARD_SUCCESS;
	}

	/**
	 * 将商品所有的分类题取出,装载在相应的区域
	 */
	@Override
	public String execute(ActionForm from, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("utf-8");
		List<Category> categories = cs.findAllCategory();
		request.setAttribute("categories", categories);
		return Constants.FORWARD_SUCCESS;
	}

	/**
	 * 获得分类详情
	 * 
	 * @param from
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public String getDetail(ActionForm from, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("utf-8");
		Integer id = Integer.parseInt(request.getParameter("id"));
		Integer pid = Integer.parseInt(request.getParameter("pid"));
		request.setAttribute("id", id);
		request.setAttribute("pid", pid);
		return Constants.FORWARD_SUCCESS;
	}

	/**
	 * 新书请求<br>
	 * :将新书添加到主界面中
	 * 
	 * @param from
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public String getNewBook(ActionForm from, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO
		return Constants.FORWARD_SUCCESS;
	}

	/**
	 * 推荐图书请求<br>
	 * :将推荐图书添加到主界面中
	 * 
	 * @param from
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public String getRecommend(ActionForm from, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		return Constants.FORWARD_SUCCESS;
	}

	/**
	 * 详细分类图书显示
	 * 
	 * @param from
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public String categoryDetail(ActionForm from, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return Constants.FORWARD_SUCCESS;
	}

}
